package com.meihua.shellapp.activity.personalcenter;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aspsine.irecyclerview.IRecyclerView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.meihua.shellapp.R;
import com.meihua.shellapp.adapter.personalcenter.MyBrowseAdapter;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.bean.BrowsingBean;
import com.meihua.shellapp.databean.BrowsingDataBean;
import com.meihua.shellapp.utils.MyStringCallBack;
import com.meihua.shellapp.utils.ToastUtils;
import com.meihua.shellapp.utils.URL;
import com.meihua.shellapp.view.footer.LoadMoreFooterView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 作者：lc on 2017/9/27.
 * 邮箱：lc0607live@163.com
 */

public class MyBrowseActivity extends BaseActivity {

    @Bind(R.id.rv_browse)
    IRecyclerView rv_browse;//列表
    @Bind(R.id.iv_top_left)
    ImageView iv_top_left;//返回
    @Bind(R.id.tv_title)
    TextView tv_title;//标题

    private List<BrowsingDataBean> data = new ArrayList<>();
    private LoadMoreFooterView loadMoreFooterView;
    private MyBrowseAdapter mAdapter;
    private int page = 1;

    @Override
    public int getLayoutId() {
        return R.layout.activity_mybrowse;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        iv_top_left.setVisibility(View.VISIBLE);
        iv_top_left.setOnClickListener(this);
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText("我的浏览");
        intList();
    }

    @Override
    public void createLoadData() {
        getListData();
    }

    /**
     * 初始化列表
     */
    private void intList() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        rv_browse.setLayoutManager(layoutManager);
        loadMoreFooterView = (LoadMoreFooterView) rv_browse.getLoadMoreFooterView();
        mAdapter = new MyBrowseAdapter(this, data);
        rv_browse.setIAdapter(mAdapter);
        rv_browse.setOnLoadMoreListener(this);
        rv_browse.setOnRefreshListener(this);
    }

    @Override
    public void onLoadMore() {
        if (loadMoreFooterView.canLoadMore() && mAdapter.getItemCount() > 0) {
            loadMoreFooterView.setStatus(LoadMoreFooterView.Status.LOADING);
            page++;
            getListData();
        }
    }

    @Override
    public void onRefresh() {
        page = 1;
        getListData();
    }

    /**
     * 获取列表数据
     */
    private void getListData() {
        OkGo.<String>post(URL.getUrl().GETMYBROWSE)
                .params("mid", MyApplication.getInstance().getMId())
                .params("page", page)
                .execute(new MyStringCallBack<BrowsingBean>(rv_browse) {
                    @Override
                    public void onSuccessData(BrowsingBean bean) {
                        super.onSuccessData(bean);
                        if (bean.getData() != null && bean.getData().size() > 0) {
                            if (page == 1) {
                                data.clear();
                            }
                            data.addAll(bean.getData());
                            mAdapter.notifyDataSetChanged();
                        } else {
                            if (page == 1) {
                                data.clear();
                                mAdapter.notifyDataSetChanged();
                            }
                            endRefreshing(rv_browse);
                            ToastUtils.getInstance().showToast(bean.getMsg());
                        }
                    }

                });
    }
}
